package cn.pku.sirx.snad.uias.util;

import java.util.Collection;
import java.util.Comparator;

import org.springframework.util.Assert;

/**
 * @author WangXiao
 */
public abstract class AnalyzerUtils {

	/**
	 * Get the multiplication result of given vectors.
	 * 
	 * @param weightVector
	 *            weight vector
	 * @param valueVector
	 *            value vector
	 * @return multiplication result of given vectors
	 */
	public static double analyzeByWeight(double[] weightVector,
			double[] valueVector) {
		Assert.notNull(weightVector);
		Assert.notNull(valueVector);

		double result = .0;
		for (int i = 0; i < valueVector.length; i++) {
			try {
				result += weightVector[i] * valueVector[i];
			} catch (Exception e) {
				throw new IllegalArgumentException(
						"weights and values must have the same length");
			}
		}
		return result;
	}

	/**
	 * Get count of common elements in two collections.
	 * 
	 * @param <T>
	 *            element type of collections
	 * @param collection1
	 *            if null return 0
	 * @param collection2
	 *            if null return 0
	 * @param comparator
	 *            a comparator of T
	 * @return count of common elements in two collections
	 */
	public static <T> int getCommonCount(Collection<T> collection1,
			Collection<T> collection2, Comparator<T> comparator) {
		if (collection1 == null || collection2 == null) {
			return 0;
		}
		int result = 0;
		for (T element1 : collection1) {
			for (T element2 : collection2) {
				boolean equals = comparator == null ? element1.equals(element2)
						: comparator.compare(element1, element2) == 0;
				if (equals) {
					result += 1;
				}
			}
		}
		return result;
	}

}
